﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Net.Sockets; 

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //private string GetPublicIP();

       

       

        private void GetLocalIP()
        {
            IPHostEntry local = Dns.GetHostEntry(name.Text);
            for (int index = 0; index < local.AddressList.Length; index++)
            {
                string localIP = Convert.ToString(local.AddressList[index]);
                lip.Text += localIP;
                lip.Text += "/n";
                //int a = localIP.IndexOf("/n") + 3;
                //int b = localIP.IndexOf("n/");
                //localIP = localIP.Substring(a, b - a);
                //lip.Text = localIP;
            }
        }
        private string GetGlobalIP()
        {
            WebRequest wr = WebRequest.Create("http://checkip.dyndns.org");
            WebResponse wr2 = wr.GetResponse();
            StreamReader sr = new StreamReader(wr2.GetResponseStream());
            string st = sr.ReadToEnd();
            int a = st.IndexOf("s: ") + 3;
            int b = st.IndexOf("</b");
            st = st.Substring(a, b - a);
            return st;
        }

        private void Enter_Click(object sender, EventArgs e)
        {
            GlobalIP.Text = GetGlobalIP();
            name.Text = Dns.GetHostName();
            GetLocalIP();

        }
        //Конец настроек

        TcpListener listener;
        string inmessege;

        private void StartListening()
        {
             int port = Convert.ToInt32(MyPort.Text);
             listener = new TcpListener (IPAddress.Any, Convert.ToInt32(MyPort.Text) );
             listener.Start();
             bw.RunWorkerAsync();
        }

        private void Listen_Click(object sender, EventArgs e)
        {
            StartListening();
        }

        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            TcpClient client = listener.AcceptTcpClient();
            StreamReader reader = new StreamReader(client.GetStream());
            inmessege = reader.ReadToEnd(); reader.Close();
        }

        private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Messages.Items.Add(">" + inmessege);
            bw.RunWorkerAsync();
        }
        private void Send()
        {
            try
            {
                TcpClient Client = new TcpClient();
                Client.Connect(IPAddress.Parse(ipSend.Text), Convert.ToInt32(portSend.Text));
                StreamWriter write = new StreamWriter(Client.GetStream());
                String current_time_str = DateTime.Now.ToString("HH:mm:ss");
                write.WriteLine(Sendtext.Text);
                write.WriteLine(current_time_str);
                write.WriteLine(ChatName.Text);
                write.Close(); 
            }
            catch
            {

            }
        }

        private void sendmesseges_Click(object sender, EventArgs e)
        {
            Send();
        }

    }
}  
    

